home *** CD-ROM | disk | FTP | other *** search
- /* >C.Xcopy */
-
- /******************************************************************************
- XCOPY (c) D.J.Scott 1988
- *******************************************************************************
- Program to perform selective backups of a hard disc
-
- Syntax: *XCOPY [<source path name>] [<destination path name>] [options]
- Default source path name is :4.$
- Default destination path name is :0.$
- Options are:
- -c confirm before each item is copied or deleted from destination
- -f full copy required (default copies only files which have changed)
- -r cancel recursive option so that subdirectories are not copied
- -v cancel verbose option to suppress display information
- ******************************************************************************/
-
- static char *progid = "XCOPY 1.05 - 27/01/89";
-
- /*************************** INCLUDE DIRECTIVES ******************************/
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <signal.h>
- #include <arthur.h>
-
- /*************************** DEFINE DIRECTIVES *******************************/
-
- #define ADFS_FreeSpace 0x40243
- #define FALSE 0
- #define TRUE 1
- #define OPTTOT 4
-
- /*************************** GLOBAL VARIABLES ********************************/
-
- int copt = 0; /* confirm option */
- int fopt = FALSE; /* full copy required option */
- int ropt = 1; /* recursive option */
- int vopt = 272; /* verbose option */
-
- char sourc[120]; /* source path name */
- char destn[120]; /* destination path name */
-
- static char *dsourc = ":4"; /* default source path name */
- static char *ddestn = ":0"; /* default destination path name */
- static char *sep = "."; /* wild card default */
- static char *wild = "*"; /* wild card default */
-
- static char *optlist[OPTTOT] = /* option list */
- {
- "-c", /* current directory only */
- "-f", /* force overwrite and delete option */
- "-r", /* recursive option */
- "-v", /* verbose option */
- };
-
- /*************************** FUNCTION DEFINITIONS ****************************/
-
- void chkerr(struct error *);
- void copyfile(char *, char *);
- void escape(int);
- void freespc(char *);
- void helptext(void);
- void readdir(char *, char *);
- void setoption(char *);
- void wipepath(char *);
-
- /******************************************************************************
- * main main program
- * argc number of command line parameters
- * argv[] array of parameter string pointers
- ******************************************************************************/
- int main(argc, argv)
- int argc; /* count of command line arguments */
- char *argv[]; /* pointers to argument strings */
- {
- static char *optchar = "-"; /* option command introducer */
- int i = 1;
-
- signal(SIGINT, escape);
-
- /* process command line */
- if (argc == 1)
- {
- /* null parameters, print program identity and help text */
- helptext();
- exit(0);
- return(0);
- }
-
- while (argc > i)
- {
- if (strncmp(optchar, argv[i], 1) == 0)
- {
- setoption(argv[i]);
- }
- else if (i == 1)
- {
- /* source name found */
- if (strncmp(argv[1], ":", 1) != 0)
- {
- /* insert default drive if drive not specified */
- strcpy(sourc, dsourc);
- strcat(sourc, sep);
- strcat(sourc, argv[1]);
- }
- else
- strcpy(sourc, argv[1]);
- }
- else if (i == 2)
- {
- /* destination name found */
- if (strncmp(argv[2], ":", 1) != 0)
- {
- /* insert default drive if drive not specified */
- strcpy(destn, ddestn);
- strcat(destn, sep);
- strcat(destn, argv[2]);
- }
- else
- strcpy(destn, argv[2]);
- }
- else if (i > 2)
- {
- fprintf(stderr, "** Unknown command parameter\n\a");
- helptext();
- exit(1);
- }
- i++;
- }
-
- /* set default source and destination path names */
- if (strlen(sourc) == 0)
- strcpy(sourc, dsourc);
- if (strlen(destn) == 0)
- strcpy(destn, ddestn);
- printf("\n");
- if (fopt == TRUE)
- {
- strcat(sourc, sep);
- strcat(sourc, wild);
- strcat(destn, sep);
- strcat(destn, wild);
- wipepath(destn);
- copyfile(sourc, destn);
- }
- else
- readdir(sourc, destn);
- destn[2] = 0; /* truncate destination to give drive */
- freespc(destn);
- exit(0);
- return(0);
- }
-
- /******************************************************************************
- chkerr checks for Operating System error and prints details if found
- ******************************************************************************/
- void chkerr(e)
- struct error *e;
- {
- int errno;
-
- if (e != 0)
- {
- errno = e->errnum & 0xFFF;
- fprintf(stderr, "\n** %s Error &%x\n\a", e->errmess, errno);
- exit(1); /* terminate as fatal error */
- }
- return;
- }
-
- /******************************************************************************
- * copyfile copy source file to destination path
- * sourc source wildcarded path name string pointer
- * destn destination wildcarded path name string pointer
- ******************************************************************************/
- void copyfile(sourc, destn)
- char *sourc;
- char *destn;
- {
- reg_set cy;
-
- cy.r[0] = 26;
- cy.r[1] = (int)sourc; /* source path name */
- cy.r[2] = (int)destn; /* destination path name */
- cy.r[3] = 2 + copt + ropt + vopt; /* action mask (F plus optionally CRV) */
- chkerr(swix(OS_FSControl, &cy));
- return;
- }
-
- /******************************************************************************
- escape
- ******************************************************************************/
- void escape(signo)
- int signo;
- {
- signal(signo, SIG_IGN);
- printf("\nEscape\n");
- exit(1);
- return;
- }
-
- /******************************************************************************
- freespc obtain and print the amount of free space left on the disc
- disc string giving the disc identity
- ******************************************************************************/
- void freespc(disc)
- char *disc;
- {
- reg_set fs;
-
- fs.r[0] = (int)disc;
- chkerr(swix(ADFS_FreeSpace, &fs));
- printf(" %d bytes free on disc %s, largest space %d bytes\n", fs.r[0], disc, fs.r[1]);
- return;
- }
-
- /******************************************************************************
- * helptext outputs help text
- ******************************************************************************/
- void helptext()
- {
- printf("%30s (c) 1988 D.J.Scott\n", progid);
- printf("Selective backup program\n\n");
- printf("Syntax: *XCOPY [<source path>] [<destination drive>] [options]\n");
- printf(" Default source is :4\n Default destination is 0:\n");
- printf("Options are:\n");
- printf(" -c confirm before each item is copied\n");
- printf(" -f full copy, default copies only files which have changed\n");
- printf(" -r cancel recursive option, subdirectories are not copied\n");
- printf(" -v cancel verbose option, no information given about copies\n");
- return;
- }
-
- /******************************************************************************
- * readdir reads and displays directory contents
- * snam source directory path name string pointer
- * dnam destination directory path name string pointer
- ******************************************************************************/
- void readdir(snam, dnam)
- char *snam;
- char *dnam;
- {
- char spath[250]; /* source path name string */
- char dpath[250]; /* destination path name string */
-
- struct dirlist /* directory list record structure */
- {
- int loadaddr; /* load address */
- int execaddr; /* execution address */
- int filelength; /* file length */
- int fileattrib; /* file attributes */
- int objtype; /* object type */
- char objname[12]; /* object name */
- } dirlist;
-
- osgbpb_block db;
- osfile_block dfblk, *d;
- struct dirlist *s; /* source file details pointer */
-
- s = &dirlist;
- d = &dfblk;
- /* read name and details of each item in directory */
- /* set up data block for osgbpb to read directory contents */
- db.action = 10; /* read directory entries */
- db.file_handle = (int)snam; /* pointer to directory name */
- db.data_addr = &dirlist; /* address of data */
- db.number = 1; /* number of object names to read */
- db.buf_len = sizeof(dirlist); /* buffer length */
- db.wild_fld = wild; /* wildcard name to match */
- db.seq_point = 0; /* offset of next item in directory */
- do
- {
- chkerr(osgbpb(&db)); /* read directory */
- if (db.number != 0 && db.seq_point >= 0)
- {
- if (s->objtype == 1) /* file */
- {
- strcpy(spath, snam); /* form full source file name */
- strcat(spath, sep);
- strcat(spath, s->objname);
- strcpy(dpath, dnam); /* form full destination file name */
- strcat(dpath, sep);
- strcat(dpath, s->objname);
-
- /* read data from file destination */
- d->action = 5;
- d->name = dpath;
- chkerr(osfile(d));
- if (d->action == 0)
- copyfile(spath, dpath);
- else if (d->action == 1)
- {
- if (d->loadaddr != s->loadaddr || d->execaddr != s->execaddr ||
- d->start != s->filelength || d->end != s->fileattrib)
- copyfile(spath, dpath);
- }
- }
- }
- }
- while (db.seq_point >= 0);
-
- if (ropt == 1)
- {
- /* read each subdirectory and copy as well */
- /* set up data block for osgbpb to reread directory contents */
- db.number = 1; /* number of object names to read */
- db.wild_fld = wild; /* wildcard name to match */
- db.seq_point = 0; /* offset of next item in directory */
- do
- {
- chkerr(osgbpb(&db)); /* read directory */
- if (db.number != 0 && db.seq_point >= 0)
- {
- if (s->objtype == 2) /* directory */
- {
- strcpy(spath, snam); /* form source subdirectory pathname */ strcat(spath, sep);
- strcat(spath, s->objname);
- strcpy(dpath, dnam); /* form destination subdirectory pathname */
- strcat(dpath, sep);
- strcat(dpath, s->objname);
-
- /* check if directory exists */
- d->action = 5;
- d->name = dpath;
- chkerr(osfile(d));
- if (d->action != 2)
- {
- /* create the directory */
- d->action = 8;
- d->name = dpath;
- d->start = 0;
- chkerr(osfile(d));
- }
- readdir(spath, dpath);/* read directory contents */
- }
- }
- }
- while (db.seq_point >= 0);
- }
- return;
- }
-
- /******************************************************************************
- * setoption decodes the option string and sets the appropriate option flag
- * opt pointer to option parameter string
- ******************************************************************************/
- void setoption(opt)
- char *opt;
- {
- int n;
- int found = FALSE;
-
- for (n = 0; n < OPTTOT; n++)
- {
- if (strncmp(opt, optlist[n], 2) == 0)
- {
- found = TRUE;
- switch(n)
- {
- case 0 :
- copt = 8; /* turn confirm option on */
- break;
- case 1 :
- fopt = TRUE; /* turn full option on */
- break;
- case 2 :
- ropt = 0; /* turn recursive option off */
- break;
- case 3 :
- vopt = 0; /* turn verbose option off */
- break;
- }
- }
- }
- if (found == FALSE)
- {
- fprintf(stderr, "\n** Unknown option parameter\n\a");
- helptext();
- exit(1);
- }
- return;
- }
-
- /******************************************************************************
- * wipepath wipe all files from the specified path
- * path wildcarded path name string pointer
- ******************************************************************************/
- void wipepath(path)
- char *path;
- {
- osfile_block blk, *d;
- reg_set wipe;
-
- d = &blk;
- /* check if any items to delete */
- d->action = 5;
- d->name = path;
- chkerr(osfile(d));
- if (d->action != 0)
- {
- wipe.r[0] = 27;
- wipe.r[1] = (int)path; /* wildcarded path name */
- wipe.r[3] = 2 + copt + ropt + vopt; /* action mask (F and optionally CRV) */
- chkerr(swix(OS_FSControl, &wipe)); /* wipe disc */
- }
- return;
- }
-
- /* end of file */
-